// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EnvironmentVisualizationAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Manipulation.Visualization
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Windows.Media;
    using System.Windows.Media.Media3D;

    using Microsoft.Robotics.Geometry;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// A class that translates visualization messages into a concrete scene
    /// for display
    /// </summary>
    [DataContract]
    public class EnvironmentVisualizationAgent : Agent,
        IControlReceiver<VisualizeGeometryElementGroup3DMessage>
    {
        /// <summary>
        /// name of the visualization agent
        /// </summary>
        [DataMember]
        private string visualizationAgentName;

        /// <summary>
        /// environment agent
        /// </summary>
        private IAgentHost visualizationAgent;

        /// <summary>
        /// Initializes a new instance of the <see cref="EnvironmentVisualizationAgent" /> class.
        /// </summary>
        /// <param name="name">name of this agent</param>
        /// <param name="visualizationAgentName">name of the visualization agent</param>
        public EnvironmentVisualizationAgent(string name, string visualizationAgentName)
            : base(name)
        {
            this.visualizationAgentName = visualizationAgentName;
        }

        /// <summary>
        /// Initialization routine
        /// </summary>
        /// <param name="locator">agent locator</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.visualizationAgent = this.Locator.GetAgent(this.visualizationAgentName);
        }

        /// <summary>
        /// When a union of spheres are given, this synthesizes a scene for visualization
        /// </summary>
        /// <param name="message">message received</param>
        public void ReceiveControl(VisualizeGeometryElementGroup3DMessage message)
        {
            Model3DGroup env = new Model3DGroup();

            for (int i = 0; i < message.ElementGroup.ElementGroup.Count; ++i)
            {
                GeometryElement3D element = message.ElementGroup.ElementGroup[i];
                if (element is SphereElement)
                {
                    SphereElement sphere = (SphereElement)element;
                    Matrix4 transf = sphere.ReferenceFrame * HomogenousTransform.CreateFromTranslation(sphere.Location);
                    Point3D center = new Point3D(transf[0, 3], transf[1, 3], transf[2, 3]);

                    env.Children.Add(GeometryManipulator.CreateASphere(center, sphere.Radius, Colors.Aqua));
                }
                else
                {
                    throw new NotImplementedException("Only sphere elements are supported at this moment");
                }
            }

            UpdateStaticEnvironmentMessage msg = new UpdateStaticEnvironmentMessage(env);
            msg.ObjectGroup.Freeze();
            this.visualizationAgent.GetControlPort<UpdateStaticEnvironmentMessage>().Post(msg);
        }
    }
}
